// ignore_for_file: constant_identifier_names

import 'package:flutter/material.dart';
import 'package:get/get_connect/http/src/response/response.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:verse_systems/utils/sp_util.dart';
import 'dart:convert' as convert;

class AccountUtil {
  static const String app_is_first_open = "app_is_first_open";
  static const String app_token = "app_token";
  static const String user_info = "user_info";
  static const String app_baseUrl = "app_baseUrl";
  static const String app_languages = "app_languages";

  // 设置 App状态为非首次启动,用于判断是否显示协议条款对话框等
  static Future<bool?> setAppHasBeenOpened() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return await sp.setBool(app_is_first_open, false);
  }

  // 获取 App是否为首次启动,用于判断是否显示协议条款对话框等
  static Future<bool> getAppIsFirstOpen() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    bool ifirstOpen = sp.getBool(app_is_first_open) ?? true;
    return ifirstOpen;
  }

  // 获取当前用户登陆状态
  static Future<bool> getLoginStatus() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    String isHaveToken = sp.getString(app_token) ?? "";
    if (isHaveToken.isEmpty) {
      debugPrint("AccountUtil: 用户登陆状态:false");
      return false;
    }
    debugPrint("AccountUtil: 用户登陆状态:true");
    return true;
  }

  // 设置 token
  static Future<bool> setUserToken(String token) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return await sp.setString(app_token, token);
  }

  // 获取 token
  static Future<String> getUserToken() async {
    if (SPUtil.getString(app_token) == null) {
      debugPrint("AccountUtil: 【token】为空");
    } else {
      debugPrint("AccountUtil: 已获取到【token】");
    }
    return await SPUtil.getString(app_token);
  }

  // 缓存app_languages
  static Future<bool> setAppLanguages(String languages) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return await sp.setString(app_languages, languages);
  }

  //获取缓存语言
  static Future<String> getAppLanguages() async {
    final languages = await SPUtil.getString(app_languages);
    if (languages == null) {
      debugPrint("AccountUtil: 获取缓存语言为空");
      return "CN";
    }
    debugPrint("AccountUtil: 获取缓存语言 $languages");
    return await SPUtil.getString(app_languages);
  }

  // 缓存域名BaseUrl
  static Future<bool> setAppBaseUr(String baseUrl) async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return await sp.setString(app_baseUrl, baseUrl);
  }

  /// 清除缓存域名BaseUrl
  static Future<bool> clearBaseUrl() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.remove(app_baseUrl);
    debugPrint("AccountUtil: 清除缓存域名 已清空");
    return true;
  }

  //获取缓存域名BaseUrl
  static Future<String> getAppBaseUrl() async {
    final baseUrl = await SPUtil.getString(app_baseUrl);
    if (baseUrl == null) {
      debugPrint("AccountUtil: 缓存域名BaseUrl为空");
      return "";
    }
    debugPrint("AccountUtil: 缓存域名 $baseUrl");
    return await SPUtil.getString(app_baseUrl);
  }

  /// 清除存储
  static Future<bool> clearAll() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    await sp.remove(app_token);
    await sp.remove(user_info);
    debugPrint("AccountUtil: 本地存储的所有信息，已清空");
    return true;
  }

  static Future<bool> saveCache(
      {required String key, required final data}) async {
    if (data != null) {
      debugPrint("saveCache : key $key data:$data");
      SharedPreferences sp = await SharedPreferences.getInstance();
      String jsonString = convert.jsonEncode(data);
      return await sp.setString(key, jsonString);
    } else {
      debugPrint("UserInfo == null");
    }
    return false;
  }

  //获取用户信息
  static Future getUserCache({required String key}) async {
    SharedPreferences sp = await SharedPreferences.getInstance();

    String stringCache = sp.getString(key) ?? "";
    debugPrint("getUserCache: String : $stringCache");
    if (stringCache.isNotEmpty) {
      Response<dynamic> jsonCode = convert.jsonDecode(stringCache);
      return jsonCode;
    }
    return null;
  }
}
